package com.yuma.stream;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        List<String> list = List.of("hello kitty", "hello world","hello world","i love java", "heihei", "haha", "yumayuma");
        // 升序排序
//        list.stream().sorted().forEach(System.out::println);
        // 元素个数
//        System.out.println(list.stream().count());
        // 去重
//        list.stream().distinct().forEach(System.out::println);

//        list.stream().skip(2).forEach(System.out::println);

//        list.stream().limit(3).forEach(System.out::println);

//        List<Integer> list2 = List.of(1,2,3,4,5,6,7,8,9);
//        // BiFunction<T,U,R> R apply(T t, U u);
//        System.out.println(list2.stream().reduce((a, b) -> a * b).orElse(0)); // a=1,b=2 return 3  a=3,b=3 return 6 a=6,b=4 return 10

        // Function<T,R>  "abc".toUpperCase()
        // 已存在的函数可以给Function就用 函数引用，如果不能满足需求就要使用lambda
//        list.stream().map(String::toUpperCase).forEach(System.out::println);
//        list.stream().map(str -> str.substring(0,3)).forEach(System.out::println);

        // Predicate<T> boolean test(T t);
//        list.stream().filter(String::isEmpty).forEach(System.out::println);
//        list.stream().filter(str -> str.contains("hello")).forEach(System.out::println);

        // 收集
        List<Student> stuList = List.of(new Student(1,"zs","male",100),
                new Student(2,"lisi","male",90),
                new Student(3,"wangwu","female",80),
                new Student(4,"maliu","female",50),
                new Student(5,"xiaoming","female",60),
                new Student(6,"fangfang","male",40),
                new Student(7,"xiaoli","female",20),
                new Student(8,"daha","male",10)
        );

//        Map<String, List<Student>> map = stuList.stream().collect(Collectors.groupingBy(Student::getSex));
//        map.forEach((k,v) -> System.out.println(k + ":\n" + v));

//        Map<String, Long> collect = stuList.stream().collect(Collectors.groupingBy(Student::getSex,
//                                                                                   Collectors.counting()));
//        collect.forEach((k,v) -> System.out.println(k + ":" + v));

//        Map<String, Integer> collect = stuList.stream()
//                .collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(Student::getScore)));
//        collect.forEach((k,v) -> System.out.println(k + ":" + v));

//        Map<String, Double> collect = stuList.stream()
//                .collect(Collectors.groupingBy(Student::getSex, Collectors.averagingInt(Student::getScore)));
//        collect.forEach((k,v) -> System.out.println(k + ":" + v));

        Map<String, Optional<Student>> collect = stuList.stream()
                .collect(Collectors.groupingBy(Student::getSex,
                                               Collectors.maxBy((o1, o2) -> o1.getScore() - o2.getScore())));
        collect.forEach((k,v) -> System.out.println(k + ":" + v.get()));
    }
}
